/*! lzf */

var cache = {};

function deletePic(a) {
    dialog({
        title: "删除图片",
        width: 400,
        height: 60,
        content: "图片删除了不能恢复，你确定要删除么？",
        button: [{
            value: "确定", callback: function (e) {
                var t = $(a).parent().parent();
                data = {
                    file_id: t.attr("file-id"),
                    id: $.data(cache, "apartment_id")
                }, $.post(base.site_url() + "client/memMng/memApartmentMng/removePhotos", data, function (e) {
                    0 == e.code ? (t.find("div").hasClass("show_trangle") && t.parent().find(".pic-regular").first().replaceWith(t.find(".show_trangle")), t.remove()) : base.alertMsg({text: e.message})
                }, "json")
            }, autofocus: !0
        }, {value: "取消"}]
    }).show()
}

function setPoster(a) {
    var e = $(a).parent().parent(), i = $.data(cache, "apartment_id");
    data = {
        file_id: e.attr("file-id"),
        id: i
    }, $.post(base.site_url() + "client/memMng/memApartmentMng/setDefaultPhoto", data, function (e) {
        if (0 == e.code) {
            $("#pic_apart_holder .show_trangle").replaceWith('<div class="pic-regular"><span class="icon icon-trash" onclick="deletePic(this);"></span><span class="icon-feng" onclick="setPoster(this);">封</span></div>'), $(a).parent().replaceWith('<div class="show_trangle" data-status="0"><span class="icon icon-trash" onclick="deletePic(this);"></span><div class="pic_trangle"></div><span id="feng">封</span></div>');
            var t = $(".show_trangle").parent().find("img").attr("src");
            $(".table-des[data-id=" + i + "]").parent().parent().find("img").attr("src", t)
        } else base.alertMsg({text: e.message})
    }, "json")
}

$(document).ready(function () {
    $(".publish").click(function () {
        var e = {apartment_id: $(this).parent().attr("data-id")};
        $.post(base.site_url() + "client/memMng/memApartmentMng/publish", {data: JSON.stringify(e)}, function (e) {
            0 == e.code ? (base.alertMsg({text: "发布成功"}), setTimeout(function () {
                window.location.reload()
            }, 1e3)) : base.alertMsg({text: e.message})
        }, "json")
    }), $(".unpublish").click(function () {
        var e = {apartment_id: $(this).parent().attr("data-id")};
        $.post(base.site_url() + "client/memMng/memApartmentMng/unpublish", {data: JSON.stringify(e)}, function (e) {
            0 == e.code ? (base.alertMsg({text: "取消发布成功"}), setTimeout(function () {
                window.location.reload()
            }, 1e3)) : base.alertMsg({text: e.message})
        }, "json")
    }), artForm.validate("edit_apart_form", base.validate_config("apart"), function (e, t, a) {
        var i = {}, n = {};
        "disabled" != $("#property_name").attr("disabled") && (n.tp_ppt_name = $("#property_name").val(), n.tp_region_id = $("#tp_region_name option:selected").val()), e.apartment_id = $("#property_name").attr("data-id"), i.apart = e, i.items = e.items, i.property = n, delete e.items, $.post(base.site_url() + "client/memMng/memApartmentMng/saveApartment", {data: JSON.stringify(i)}, function (e) {
            0 == e.code ? window.location.reload() : base.alertMsg({text: e.message})
        }, "json")
    }), $(".pic-apart").click(function () {
        if (base.is_ie() && base.fuck_ie_version(10) && $("#filePicker").replaceWith('<div id="filePicker"></div>'), base.is_ie() && base.fuck_ie_version(10) && base.flash_version() < 11) window.confirm("您的flash版本过低，下载最新flash") && (window.location.href = "http://w.x.baidu.com/alading/anquan_soft_down_ub/17153"); else {
            var r = $(this).parent().attr("data-id"), e = ($(this).parent().parent().attr("data-status"), {id: r});
            $.data(cache, "apartment_id", r), $.post(base.site_url() + "client/memMng/memApartmentMng/getPhotos", e, function (e) {
                if (0 == e.code) {
                    for (var t = e.data, a = null, i = "", n = 0; n < t.length; n++) a = t[n], 0 == n ? i += '<div class="pic-holder apart_pic" data-id="' + r + '" file-id="' + a.files_id + '"><div class="show_trangle"  data-status="0"><span class="icon icon-trash" onclick="deletePic(this);"></span><div class="pic_trangle"></div><span id="feng">封</span></div><img src="' + a.url_medium + '" width="230" height="160" /></div>' : 3 == a.fs_status ? i += '<div class="pic-holder apart_pic"  data-id="' + r + '"  file-id="' + a.files_id + '"><div class="pic-regular"><span class="icon icon-trash" onclick="deletePic(this);"></span></div><div class="pic_shen_trangle"></div><span class="shen">待审核</span><img src="' + a.url_medium + '"  width="230" height="160"  /></div>' : i += '<div class="pic-holder apart_pic"  data-id="' + r + '"  file-id="' + a.files_id + '"><div class="pic-regular"><span class="icon icon-trash" onclick="deletePic(this);"></span><span class="icon-feng"  onclick="setPoster(this);">封</span></div><img src="' + a.url_medium + '"  width="230" height="160"  /></div>';
                    $("#pic_apart_holder").html(i), dialog({
                        title: "图片管理",
                        width: 900,
                        content: $("#pic_apart_con")
                    }).showModal(), (s = WebUploader.create({
                        auto: !0,
                        swf: "./assets/libs/webupload/Uploader.swf",
                        server: base.site_url() + "client/memMng/memApartmentMng/webUploader/" + $.data(cache, "apartment_id"),
                        pick: "#upload_holder",
                        accept: {title: "Images", extensions: "jpg,jpeg,png", mimeTypes: "image/*"},
                        compress: {
                            width: 1200,
                            height: 1200,
                            quality: 70,
                            allowMagnify: !1,
                            crop: !1,
                            preserveHeaders: !0,
                            noCompressIfLarger: !1,
                            compressSize: 2e5
                        }
                    })).on("beforeFileQueued", function (e) {
                        var t = e.ext.toLowerCase();
                        "jpg" != t && "jpeg" != t && "png" != t && base.alertMsg({text: "图片格式不正确"})
                    }), s.on("fileQueued", function (e) {
                        $("#pic_apart_holder").append('<div class="pic-holder spinner_pic"><span class="icon-spin"></span></div>')
                    }), s.on("uploadSuccess", function (e, t) {
                        var a, i;
                        0 == t.code ? (a = t.data, $("#pic_apart_holder .spinner_pic"), 0 == $("#pic_apart_holder .apart_pic").length ? ($("#pic_apart_holder .spinner_pic").first().replaceWith('<div  data-id="' + i + '"  file-id="' + a.files_id + '" class="pic-holder apart_pic"><div class="show_trangle"  data-status="0"><span class="icon icon-trash" onclick="deletePic(this);"></span><div class="pic_trangle"></div><span id="feng">封</span></div><img src="' + a.url_medium + '" /></div>'), $(".table-des[data-id=" + i + "]").parent().parent().find("img").attr("src", a.url_medium)) : $("#pic_apart_holder .spinner_pic").first().replaceWith('<div data-id="' + i + '"  file-id="' + a.files_id + '" class="pic-holder apart_pic"><div class="pic-regular"><span class="icon icon-trash" onclick="deletePic(this);"></span><span class="icon-feng"  onclick="setPoster(this);">封</span></div><img src="' + a.url_medium + '" /></div>')) : (base.alertMsg({text: t.message}), $("#pic_apart_holder .spinner_pic").first().remove())
                    }), s.on("uploadError", function () {
                        $("#pic_apart_holder .spinner_pic").first().remove()
                    })
                } else base.alertMsg({text: e.message});
                var s
            }, "json")
        }
    }), $(".icon-reload").click(function () {
        var e = {apartment_id: $(this).attr("data-id")}, s = this;
        $.post(base.site_url() + "client/memMng/memApartmentMng/refresh", e, function (e) {
            switch (e.code) {
                case 0:
                    var t = new Date, a = t.getFullYear(), i = t.getMonth() + 1, n = t.getDate();
                    n < 10 && (n = "0" + n), i < 10 && (i = "0" + i), $(s).prev().text("更新：" + a + "-" + i + "-" + n), base.alertMsg({text: "更新成功"});
                    break;
                case 1:
                    base.alertMsg({text: e.message})
            }
        }, "json")
    })
});
var editApartHandler = function () {
    var s = function (e) {
        switch (e) {
            case"1":
                $(".zhenzu-flag").show(), $(".hezu-flag").hide();
                break;
            case"2":
                $(".zhenzu-flag").hide(), $(".hezu-flag").show()
        }
    };
    return {
        edit: function (e) {
            var t = {apartment_id: $(e).parent().attr("data-id")}, n = $("#edit_apart_form");
            $.post(base.site_url("client/memMng/memApartmentMng/getAptById"), t, function (e) {
                var t = e.data, a = t.ppt_city_id, i = template("edit_apt_tpl", {rows: t});
                n.html(i), dialog({
                    title: "编辑房源",
                    width: 900,
                    content: $("#edit_apart_form"),
                    button: [{
                        value: "确定", callback: function () {
                            return n.submit(), !1
                        }, autofocus: !0
                    }, {value: "取消"}]
                }).showModal(), n.find("#att_rent_type").change(function () {
                    s(this.value)
                }), s(e.data.att_rent_type), base.drop_tips({
                    url: base.site_url("client/search/search/getPropertyByName"),
                    input: "#property_name",
                    holder: "#edit_apart_form",
                    disPlayData: !0,
                    top: 30,
                    left: 80,
                    dataHandler: function (e) {
                        var t = {ppt_name: e};
                        return {data: JSON.stringify(t)}
                    },
                    callback: function (e, t) {
                        var a = "", i = e.data || [];
                        if (0 == e.code) for (var n = 0, s = i.length; n < s; n++) {
                            var r = i[n];
                            a += '<div class="drop-tip-item t-l" onclick="editApartHandler.selectPpt(this);" data-value="' + r.ppt_name + '"><span>' + r.ppt_name + '</span><span class="c-gray">&nbsp;&nbsp;' + r.region_name + "</span></div>"
                        }
                        t.html(a)
                    }
                }), n.find("#property_name").blur(function () {
                    var t = this;
                    setTimeout(function () {
                        if (t.value) {
                            var e = {ppt_name: t.value};
                            $.post(base.site_url("client/memMng/memApartmentMng/checkPptExisted"), e, function (e) {
                                e.data ? $("#select_region").remove() : editApartHandler.initRegion(a)
                            }, "json")
                        }
                    }, 200)
                })
            }, "json")
        }, selectPpt: function (e) {
            var t = $.trim($(e).attr("data-value"));
            $("#property_name").val(t)
        }, initRegion: function (e) {
            var t = {city_id: e};
            $.post(base.site_url("client/memMng/memApartmentMng/getRegions"), t, function (e) {
                if (0 === e.code && ($regions = e.data, 0 < !$("#select_region").length)) {
                    for (var t = 0, a = $regions, i = $regions.length, n = null, s = '<option value="">请选择</option>'; t < i; t++) s += "<option value=" + (n = a[t]).region_id + ">" + n.region_name + "</option>";
                    html = '<div class="fm-input" id="select_region">\t<span class="label">选择区域</span>\t<div class="input-wrap">        <select name="tp_region_name" id="tp_region_name" class="not-post">' + s + "</select>\t</div></div>", $("#select_ppt_wrap").after(html)
                }
            }, "json")
        }
    }
}();